﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Zor_Tools;
using Zor_Modes;
using Zor_Bll;
using Newtonsoft.Json.Linq;
using System.Web;
using System.Web.Providers.Entities;

namespace Zor_API.Controllers
{
    public class UsersController : ApiController
    {
        [HttpPost]
        public List<Users> GetUserList()
        {
            return Users_Bll.GetUserList();
        }
       [HttpPost]
        public List<Users> Userlist()
        {
            return Users_Bll.GetUserList();
        }
        [HttpPost]
        public Users UsById(int id)
        {
            return Users_Bll.GetUserById(id);
        }
        [HttpPost]
        public HttpResponseMessage UsByNameAndPass([FromBody]Users us)
        {
            us.User_Password = Encryption.GetMd5Str(us.User_Password);
            BaseAnswer bar = Users_Bll.GetUserByNameAndPass(us);
            HttpResponseMessage result = new HttpResponseMessage
            {
                Content = new StringContent(bar.ToJson(), System.Text.Encoding.GetEncoding("UTF-8"), "application/json")
            };
            return result;
        }
        [HttpPost]
        public HttpResponseMessage InserUs([FromBody]Users us)
        {
            us.User_Password = us.User_Password.Trim() == "" ? "123456" : us.User_Password;
            us.User_Password = Encryption.GetMd5Str(us.User_Password);
            us.User_CreateDate = TimeHelper.ToNowTimeStamp();
            BaseAnswer bar = new BaseAnswer();
            List<Users> user = Users_Bll.GetUserList(" and User_UserName ='" + us.User_UserName + "' and User_Id <>" + us.User_Id);
            if (user.Count > 0)
            {
                bar.AnswerType = BaseAnswer.Answers.Failure;
                bar.AnswerMeg = "用户名已存在，请重新输入！";
            }
            else
            {
                if (us.User_Id == 0)
                    bar = Users_Bll.InserUsers(us);
                else
                    bar = Users_Bll.UpdateUsers(us);
            }

            HttpResponseMessage result = new HttpResponseMessage
            {
                Content = new StringContent(bar.ToJson(), System.Text.Encoding.GetEncoding("UTF-8"), "application/json")
            };
            return result;
        }
        [HttpPost]
        public HttpResponseMessage DeUers([FromBody]JObject us)
        {
            dynamic json = us;//json["ids"][0].New_Id.ToString()

            JArray jnews = json.us;
            List<int> lst = new List<int>();
            foreach (var item in jnews)
            {
                lst.Add(item.ToObject<Users>().User_Id);
            }

            BaseAnswer bar = Users_Bll.DeleteUsers(lst.ToArray());
            HttpResponseMessage result = new HttpResponseMessage
            {
                Content = new StringContent(bar.ToJson(), System.Text.Encoding.GetEncoding("UTF-8"), "application/json")
            };
            return result;
        }
        [HttpPost]
        public string UpdateUs([FromBody]Users us)
        {
            us.User_Password = Encryption.GetMd5Str(us.User_Password);
            return Users_Bll.UpdateUsers(us).ToJson();
        }
        [HttpPost]
        public HttpResponseMessage UpdatePassWord([FromBody]Users users)
        {
            BaseAnswer bar = new BaseAnswer();
            users.User_Password = Encryption.GetMd5Str(users.User_Password);
            bar = Users_Bll.UpdateUsersPassWord(users);

            HttpResponseMessage result = new HttpResponseMessage
            {
                Content = new StringContent(bar.ToJson(), System.Text.Encoding.GetEncoding("UTF-8"), "application/json")
            };
            return result;
        }

    }
}
